﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Business.Dal;
using Business.Entity;
using System.Web.UI.MobileControls;
using System.Collections.Generic;
using System.IO;
using Sahibinden.UserControl;

namespace Sahibinden.UserAction
{
    public partial class testilanekle : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                UlkeDoldur();
                ParaDoldur();
            }

                //FileUpload myFileUpload = new FileUpload();//Yeni File Upload Kontrolünin instance’ni al
                //myFileUpload.ID = Guid.NewGuid().ToString();
                //this.pnlFileUpload.Controls.Add(myFileUpload);//Kontrolü panel içine ekle
     


               
        }

        

        private void ParaDoldur()
        {
            ParaProvider ParaSagla = new ParaProvider();
            drpCinsi.DataTextField = "ParaCinsi";
            drpCinsi.DataValueField = "ParaCinsiID";
            drpCinsi.DataSource = ParaSagla.GetAllRows();
            drpCinsi.DataBind();
        }

       
        
        
        private void UlkeDoldur()
        {

            UlkeProvider UlkeSagla = new UlkeProvider();
            drpUlke.DataTextField = "UlkeAdi";
            drpUlke.DataValueField = "UlkeID";
            drpUlke.DataSource = UlkeSagla.GetAllRows();
            drpUlke.SelectedIndex = -1;
            drpUlke.DataBind();
            drpUlke.Items.Insert(0, "Bir ülke seçiniz");

        }

        protected void drpSehir_SelectedIndexChanged(object sender, EventArgs e)
        {
            IlceProvider ilcesaglayici = new IlceProvider();
            drpIlce.DataTextField = "IlceAdi";
            drpIlce.DataValueField = "IlceID";
            drpIlce.DataSource = ilcesaglayici.IlceGetir(Convert.ToInt32(drpSehir.SelectedValue));
            drpIlce.DataBind();
        }

        protected void drpIlce_SelectedIndexChanged(object sender, EventArgs e)
        {
            MahalleProvider mahallesagla = new MahalleProvider();
            drpMahalle.DataTextField = "MahalleAdi";
            drpMahalle.DataValueField = "MahalleID";
            drpMahalle.DataSource = mahallesagla.MahalleGetir(Convert.ToInt32(drpIlce.SelectedValue));
            drpMahalle.DataBind();
        }

        protected void drpMahalle_SelectedIndexChanged(object sender, EventArgs e)
        {

        }



        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Categories.CategoryID == "0")
            {
                Response.Write("Bir kategori Seçin");
                return;
            }
            
            Ilan i = new Ilan();
            

            i.KategoriID = Categories.CategoryID;
            i.IlanBasligi = txtBaslik.Text;
            i.Aciklama = txtAciklama.Text;
            i.UlkeID = Convert.ToInt32(drpUlke.SelectedValue);
            i.SehirID = Convert.ToInt32(drpSehir.SelectedValue);
            i.MahalleID = Convert.ToInt32(drpMahalle.SelectedValue);
            i.IlceID = Convert.ToInt32(drpIlce.SelectedValue);
            i.Fiyat = Convert.ToDecimal(txtFiyat.Text);
            i.ParaCinsiID = Convert.ToInt32(drpCinsi.SelectedValue);
            i.UyeID = Convert.ToInt32(Page.User.Identity.Name);
            IlanProvider ekleyici = new IlanProvider();
            ekleyici.Insert(i);


            foreach (FileUpload item in pnlFileUpload.Controls.OfType<FileUpload>())
            {
                if (item.FileName == "")
                {
                    continue;
                }
                FileInfo fi = new FileInfo(item.FileName);
                string FileName = Guid.NewGuid().ToString() + fi.Extension;
                item.SaveAs(Server.MapPath("~/UserImages/" + FileName));
                Resim rsm = new Resim();
                rsm.IlanID = i.IlanID;
                rsm.ServerFileName = FileName;
                ResimProvider rsmp = new ResimProvider();
                rsmp.Insert(rsm);
            }


        


        }
        
        protected void drpUlke_SelectedIndexChanged(object sender, EventArgs e)
        {
            SehirProvider SehirSagla = new SehirProvider();
            drpSehir.DataTextField = "SehirAdi";
            drpSehir.DataValueField = "SehirID";
            drpSehir.DataSource = SehirSagla.SehirGetir(Convert.ToInt32(drpUlke.SelectedValue));
            drpSehir.DataBind();
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Panel1.Controls.Clear();
            //for (int i = 1; i <= Convert.ToInt32(DropDownList1.SelectedValue); i++)
            //{
            //    FileUpload myFileUpload = new FileUpload();
            //    myFileUpload.ID = "Upload_" + i;
            //    this.Panel1.Controls.Add(myFileUpload);
            //}
            //DropDownList1.Dispose();

        }
       
        
        FileUpload myFileUpload;        
        protected void Button2_Click(object sender, EventArgs e)
        {
            //for (int i = 1; i <= Convert.ToInt32(DropDownList1.SelectedValue); i++)
            //{

            //    myFileUpload = ((FileUpload)Panel1.FindControl("upload_" + i.ToString()));
            //    if (myFileUpload.HasFile)
            //    {
            //        myFileUpload.SaveAs(Server.MapPath("../Upload/" + myFileUpload.FileName));
            //        Response.Write(i.ToString() + ". Dosya " + " Upload Edilen Dosya Adı:" + myFileUpload.FileName + "<br/>");
                    
            //    }
            //}   

            FileUpload myFileUpload = new FileUpload();//Yeni File Upload Kontrolünin instance’ni al
            myFileUpload.ID = Guid.NewGuid().ToString();
            this.pnlFileUpload.Controls.Add(myFileUpload);//Kontrolü panel içine ekle

        }



      

    }


}
